﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using RolePlayingGameData;
namespace WindowsGame2.Weapon
{
    class SpellObject
    {
        RolePlayingGameData.Spell m_spellData;
        ContentManager m_content = null;
        Viewport m_viewport;
        Vector2 m_worldPosition;
        Vector2 m_startPosition;
        float m_lifeTime = 0f;
        float m_speed = 600f;
        int m_overlayLifeTime = 0;
        WindowsGame2.Characters.PlayerObject m_target = null;

        enum State
        {
            Traveling,
            Impact,
        }

        State m_state = State.Traveling;
        
        public void Initialize(ContentManager content, Viewport viewport)
        {
            m_content = content;
            m_viewport = viewport;
        }

        public void LoadContent(string contentName)
        {
            RolePlayingGameData.Spell spell = m_content.Load<RolePlayingGameData.Spell>(Path.Combine(@"Spells", contentName));
            m_spellData = spell.Clone() as RolePlayingGameData.Spell;
        }

        public void Draw(SpriteBatch spriteBatch, float elapsedGameTimeInSeconds, Vector2 mapOriginPosition)
        {
            Vector2 screenPosition = m_worldPosition + mapOriginPosition;

            if (m_target != null)
            {
                m_spellData.SpellSprite.UpdateAnimation(elapsedGameTimeInSeconds);
                m_spellData.SpellSprite.Draw(spriteBatch, screenPosition, 1f - screenPosition.Y / (float)m_viewport.Height);
            }

            if (0 < m_overlayLifeTime)
            {
                if (m_overlayLifeTime < 50)
                {
                    m_spellData.Overlay.UpdateAnimation(elapsedGameTimeInSeconds);
                    m_spellData.Overlay.Draw(spriteBatch, screenPosition, 0f);
                }
               
                --m_overlayLifeTime;
            }
                
        }

        public void Update(float elapsedGameTimeInSeconds)
        {
            if (m_target == null)
            {
                return;
            }

            switch (m_state)
            {
                case State.Traveling:
                    {
                        // get a center position of the target
                        Vector2 targetWorldPosition = m_target.GetWorldPosition();
                        targetWorldPosition += 36*Vector2.One;
                        
                        Vector2 delta = targetWorldPosition - m_startPosition;

                        float t = m_lifeTime / Math.Max(0.1f, Math.Min(1, delta.Length() / m_speed));

                        // check if the time is expired
                        if (t >= 1)
                        {
                            m_state = State.Impact;
                            m_spellData.SpellSprite.PlayAnimation(m_state.ToString());

                            m_spellData.Overlay.PlayAnimation(0);
                            m_spellData.Overlay.ResetAnimation();
                            
                            m_overlayLifeTime = 55;
                            return;
                        }

                        m_lifeTime += elapsedGameTimeInSeconds;

                        // Bezier3
                        Vector2 dir = Vector2.Normalize(delta);
                        Vector2 temp = new Vector2(dir.Y, dir.X);
                        dir = temp;
                        m_worldPosition = (targetWorldPosition) * t * t + (m_startPosition + dir * (delta) / 3) * (2 * t * (1 - t)) + (m_startPosition) * ((1 - t) * (1 - t));                        
                       
                    }break;
                case State.Impact:
                    {
                        if (m_spellData.SpellSprite.IsPlaybackComplete)
                        {
                            m_target = null;
                        }
                    }break;
            }
            
        }

        public void SetTarget(Vector2 worldPosition, WindowsGame2.Characters.PlayerObject target)
        {
            m_state = State.Traveling;
            m_spellData.SpellSprite.ResetAnimation();
            m_spellData.SpellSprite.PlayAnimation(m_state.ToString());
           

            m_worldPosition = worldPosition;
            m_startPosition = worldPosition;
            m_target = target;
            m_lifeTime = 0f;
            m_overlayLifeTime = 0;
            
        }
    }
}
